<template>
  <div id="app">
    <a-input placeholder="请输入任务" :value="inputValue" class="my_ipt" @change="handleInputChange"></a-input>
    <a-button type="primary" @click="addItemToList">添加事项</a-button>

    <a-list bordered :dataSource="infoList" class="dt_list">
      <a-list-item slot="renderItem" slot-scope="item">
        <!-- 复选框 -->
        <a-checkbox :checked="item.done" @change="(e) => {checkBoxChanged(e,item.id)}">{{item.info}}</a-checkbox>
        <!-- 删除按钮 -->
        <a slot="actions" @click="deleteItemById(item.id)">删除</a>
      </a-list-item>

      <!-- footer区域 -->
      <div slot="footer" class="footer">
        <!-- 未完成任务个数 -->
        <span>{{undoneLength}}条剩余</span>
        <!-- 操作按钮 -->
        <a-button-group>
          <a-button :type="viewKey === 'all'?'primary':'default'" @click="changeKey('all')">全部</a-button>
          <a-button
            :type="viewKey === 'undone'?'primary':'default'"
            @click="changeKey('undone')"
          >未完成</a-button>
          <a-button :type="viewKey === 'done'?'primary':'default'" @click="changeKey('done')">已完成</a-button>
        </a-button-group>
        <a @click="clearDone">清除已完成</a>
      </div>
    </a-list>
  </div>
</template>

<script>
import { mapState, mapMutations, mapGetters } from 'vuex'
export default {
  name: 'app',
  data () {
    return {}
  },
  methods: {
    ...mapMutations(['setInputValue', 'addItem', 'keyChange', 'clearDoneState', 'deleteThis', 'checkStatusChanged']),
    // 监听文本框的变化
    handleInputChange (e) {
      this.setInputValue(e.target.value)
    },
    // 向列表中新增item项
    addItemToList () {
      if (this.inputValue.trim().length <= 0) {
        return this.$message.warning('文本框内容不能为空')
      }
      this.addItem()
    },
    changeKey (key) {
      this.keyChange(key)
    },
    // 清除已完成的任务
    clearDone () {
      this.clearDoneState()
    },
    // 删除该任务
    deleteItemById (id) {
      this.deleteThis(id)
    },
    // 监听checkbox的改变事件
    checkBoxChanged (e, id) {
      const params = { id: id, checked: e.target.checked }
      this.checkStatusChanged(params)
    }
  },
  created () {
    this.$store.dispatch('getList')
  },
  computed: {
    ...mapState(['inputValue', 'viewKey']),
    ...mapGetters(['undoneLength', 'infoList'])
  }
}
</script>

<style>
#app {
  padding: 10px;
}
.my_ipt {
  width: 500px;
  margin-right: 10px;
}
.dt_list {
  width: 500px;
  margin-top: 10px;
}
.footer {
  display: flex;
  justify-content: space-between;
  align-items: center;
}
</style>
